/**
 * Created by TangZK on 2015/5/15.
 */
var mysql = require('mysql');

var db = (function () {
	var sql_host = 'localhost';
	var sql_user = 'root';
	var sql_pass = '123456';
	var sql_database = 'card4china';
	var sql_conn = {host: sql_host, user: sql_user, password: sql_pass, database: sql_database};
	var link = null;
	return {
		link: function () {
			if (link == null) {
				link = mysql.createConnection(sql_conn);
			}
		},
		add: function (sql, data, callback) {
			var qry = link.query(sql, data, callback);
			console.log(qry.sql);
		},
		select: function (sql, whereArr, callback) {
			var qry;
			if (whereArr) {
				qry = link.query(sql, whereArr, callback);
			} else {
				qry = link.query(sql, callback);
			}
			console.log(qry.sql);
		},
		update: function (sql, whereArr, callback) {
			var qry = link.query(sql, whereArr, callback);
			console.log(qry.sql);
		},
		unlink: function () {
			if (link) {
				link.end(function (err) {
					link = null;
					if (err) {
						console.log(err);
					}
				});
			}
		}
	}
})();

function KeFuModel() {
	this.name = 'kacn_kefu_model';
}

KeFuModel.prototype.getGlobalConfig = function (fn) {
	var sql = 'SELECT config_id,config_key,config_val FROM ecs_kefu_config';
	db.link();
	db.select(sql, '', function (err, result) {
		if (err) throw err;
		if (result) {
			fn && fn.call(null, result);
		}
	});
};

KeFuModel.prototype.getGroup = function (fn) {
	var sql = 'SELECT group_id,group_name FROM ecs_kefu_group';
	db.link();
	db.select(sql, '', function (err, result) {
		if (err) throw err;
		if (result) {
			fn && fn.call(null, result);
		}
	});
};

KeFuModel.prototype.getAdminInfo = function (whereArr, fn) {
	var sql = 'SELECT AU.user_id,AU.user_name,AU.email,AU.add_time,AU.action_list,KI.job_number,KI.group_id,KI.welcome,KI.status,KI.lastlogintime,KI.answer_number,KI.job_sort FROM ecs_admin_user AU LEFT JOIN ecs_kefu_info KI ON KI.user_id=AU.user_id WHERE AU.user_id=? LIMIT 1';
	db.link();
	db.select(sql, whereArr, function (err, result) {
		db.unlink();
		if (err) throw err;
		if (result && result[0]) {
			fn && fn.call(null, result[0]);
		}
	});
};

KeFuModel.prototype.createTalk = function (data, fn) {
	var sql = 'INSERT INTO ecs_kefu_talk SET ?';
	db.link();
	db.add(sql, data, function (err, result) {
		db.unlink();
		if (err) throw err;
		fn && fn.call(null, result);
	});
};

KeFuModel.prototype.updateTalk = function (dataArr, fn) {
	var sql = 'UPDATE ecs_kefu_talk SET score=?,evaluation=?,unlink_time=? WHERE talk_id=?';
	db.link();
	db.update(sql, dataArr, function (err, result) {
		db.unlink();
		fn && fn.call(this, result);
	});
};

KeFuModel.prototype.recordChat = function (data, fn) {
	var sql = 'INSERT INTO ecs_kefu_chat SET ?';
	db.link();
	db.add(sql, data, function (err, result) {
		db.unlink();
		if (err) throw err;
		fn && fn.call(null, result);
	});
};

KeFuModel.prototype.getChatList = function (whereArr, fn) {
	var sql = 'SELECT * FROM ecs_kefu_chat WHERE talk_id=? ORDER BY send_time DESC LIMIT 100';
	db.link();
	db.select(sql, whereArr, function(err, result) {
		if(result) {
			fn && fn.call(this, result);
		}
	});
};

KeFuModel.prototype.closeLink = function () {
	db.unlink();
};

exports.KeFuModel = new KeFuModel();
